<template>
  <div>
       <!-- 面包屑 -->
 <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品分类</el-breadcrumb-item>
</el-breadcrumb>

<el-card>
  <el-row class="bt">
    <el-col>
      <el-button type="primary" @click="adflForm">添加分类</el-button>
    </el-col>
  </el-row>
<tree-table :data="spfenleiList" :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="#" border :show-row-hover="false">
    <template slot="isok" slot-scope="scope">
       <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color:lightgreen"></i>
       <i class="el-icon-error" v-else style="color:red"></i>
    </template>
    <template slot="order" slot-scope="scope">
       <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
       <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
       <el-tag size="mini" type="warning" v-else>三级</el-tag>
    </template>
    <template slot="opt" slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="bianjiForm(scope.row.cat_id)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="scbtn(scope.row.cat_id)">删除</el-button>
    </template>
</tree-table>
 <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 7, 9, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>

<!-- 添加分类对话框 -->
<el-dialog title="添加分类" :visible.sync="adFormVisible" width="50%" @close="guanbi">
  <el-form label-width="80px" :model="tianjiaForm" :rules="ruless" ref="addsFormRef">
    <el-form-item label="分类名称：" prop="cat_name" label-width="100px">
    <el-input v-model="tianjiaForm.cat_name"></el-input>
    </el-form-item >
    <el-form-item label="父级分类：" label-width="100px">
    <el-cascader class="with1"
    clearable
    show-all-levels
    v-model="value"
    :options="fujiFrom"
    :props="props"
    @change="handleChange"></el-cascader>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="guanbi">取 消</el-button>
    <el-button type="primary" @click="adbtn">确 定</el-button>
  </div>
</el-dialog>

<!-- 编辑分类对话框 -->
<el-dialog title="编辑分类" :visible.sync="bjFormVisible" width="50%" @close="guanbi1">
  <el-form label-width="80px" :model="bianjiform" :rules="ruless" ref="bjFormRef">
    <el-form-item label="分类名称：" prop="cat_name" label-width="100px">
    <el-input v-model="bianjiform.cat_name"></el-input>
    </el-form-item >
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="guanbi1">取 消</el-button>
    <el-button type="primary" @click="bjbtn">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      spfenleiList: [],
      fujiFrom: [],
      adFormVisible: false,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', prop: '', type: 'template', template: 'isok' },
        { label: '排序', prop: '', type: 'template', template: 'order' },
        { label: '操作', prop: '', type: 'template', template: 'opt' }
      ],
      tianjiaForm: {
        cat_name: '',
        cat_level: 0,
        cat_pid: 0
      },
      ruless: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      value: [],
      bjFormVisible: false,
      bianjiform: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品分类列表失败')
      this.spfenleiList = res.data.result
      this.total = res.data.total
      // console.log(this.total)
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getList()
    },
    async adflForm () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
      // console.log(res.data)
      this.fujiFrom = res.data
      this.adFormVisible = true
    },
    adbtn () {
      this.$refs.addsFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.tianjiaForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.getList()
        this.adFormVisible = false
        return this.$message.success('添加分类成功')
      })
    },
    guanbi () {
      this.$refs.addsFormRef.resetFields()
      this.value = []
      this.tianjiaForm.cat_pid = 0
      this.tianjiaForm.cat_level = 0
      this.adFormVisible = false
    },
    handleChange () {
      if (this.value.length > 0) {
        this.tianjiaForm.cat_pid = this.value[this.value.length - 1]
        this.tianjiaForm.cat_level = this.value.length
        return 1
      } else {
        this.tianjiaForm.cat_pid = 0
        this.tianjiaForm.cat_level = 0
      }
    },
    async scbtn (id) {
      const result = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (!result) return
      const { data: res } = await this.$http.delete(`categories/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除分类失败')
      this.getList()
      this.$message.success('删除分类成功')
    },
    async bianjiForm (id) {
      const { data: res } = await this.$http.get(`categories/${id}`)
      if (res.meta.status !== 200) return this.$message.error('获取分类信息失败')
      this.bianjiform = res.data
      this.$message.success('获取分类信息成功')
      this.bjFormVisible = true
    },
    guanbi1 () {
      this.$refs.bjFormRef.resetFields()
      this.bjFormVisible = false
    },
    bjbtn () {
      this.$refs.bjFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`categories/${this.bianjiform.cat_id}`, { cat_name: this.bianjiform.cat_name })
        if (res.meta.status !== 200) return this.$message.error('编辑分类失败')
        this.$message.success('编辑分类成功')
        this.getList()
        this.bjFormVisible = false
      })
    }
  },
  computed: {
  }
}
</script>

<style scoped lang="less">
.bt {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: center;
}
.with1 {
  width: 100%;
}
</style>
